package com.lanxiao.cloud.modules.oauth.application.assembler;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lanxiao.cloud.common.core.dto.ResultModel;
import com.lanxiao.cloud.modules.oauth.application.dto.BasicsRoleDto;
import com.lanxiao.cloud.modules.oauth.application.dto.RoleCmdAdd;
import com.lanxiao.cloud.modules.oauth.application.dto.RoleCmdUpdate;
import com.lanxiao.cloud.modules.oauth.application.dto.UserRoleDto;
import com.lanxiao.cloud.user.api.fill.BaseDtoFillHandleFactory;
import com.lanxiao.cloud.modules.oauth.application.dto.RolePageDto;
import com.lanxiao.cloud.modules.oauth.domain.model.Role;
import java.util.List;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2021-09-16
 */
@Component
public class RoleAssembler {

  private final BaseDtoFillHandleFactory<Long> fillHandleFactory;

  public RoleAssembler(BaseDtoFillHandleFactory<Long> fillHandleFactory) {
    this.fillHandleFactory = fillHandleFactory;
  }

  public ResultModel<RolePageDto> toRolePageDto(IPage<Role> page) {
    final List<RolePageDto> rolePageDtos = RoleConverter.INSTANCE.toRolePageDto(page.getRecords());
    fillHandleFactory.fillHandle(rolePageDtos);
    return new ResultModel<>(rolePageDtos, (int) page.getTotal());
  }

  public List<BasicsRoleDto> toBasicsRoleDto(List<Role> list) {
    return RoleConverter.INSTANCE.toBasicsRoleDto(list);
  }

  public Role toRole(RoleCmdAdd add) {
    return RoleConverter.INSTANCE.toRole(add);
  }

  public Role toRole(Long id, RoleCmdUpdate update) {
    Role role = RoleConverter.INSTANCE.toRole(update);
    role.setId(id);
    return role;
  }

  public List<UserRoleDto> toUserRoleDto(List<Role> list) {
    return RoleConverter.INSTANCE.toUserRoleDto(list);
  }

  @Mapper
  public interface RoleConverter {

    RoleConverter INSTANCE = Mappers.getMapper(RoleConverter.class);

    @Mappings({
        @Mapping(source = "createBy", target = "createUser.id"),
        @Mapping(source = "updateBy",target = "updateUser.id")
    })
    RolePageDto toRolePageDto(Role role);
    List<RolePageDto> toRolePageDto(List<Role> role);
    BasicsRoleDto toBasicsRoleDto(Role role);
    List<BasicsRoleDto> toBasicsRoleDto(List<Role> role);

    Role toRole(RoleCmdAdd add);
    Role toRole(RoleCmdUpdate update);

    UserRoleDto toUserRoleDto(Role role);
    List<UserRoleDto> toUserRoleDto(List<Role> role);
  }

}
